"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasChild = exports.createMediaElement = exports.createElement = exports.setStyle = exports.isHTMLElement = exports.analysisQueryString = exports.isArray = exports.isObject = exports.isString = exports.inTypes = exports.isType = exports.getType = exports.toRawType = exports.noop = exports.assign = void 0;
function assign(def = {}, origin = {}) {
    return Object.assign({}, def, origin);
}
exports.assign = assign;
function noop() { }
exports.noop = noop;
function toRawType(target) {
    return Object.prototype.toString.call(target);
}
exports.toRawType = toRawType;
function getType(target) {
    return toRawType(target).slice(8, -1).toLowerCase();
}
exports.getType = getType;
function isType(target, type) {
    return getType(target) === type;
}
exports.isType = isType;
function inTypes(target, types) {
    const type = getType(target);
    return types.includes(type);
}
exports.inTypes = inTypes;
function isString(target) {
    return getType(target) === 'string';
}
exports.isString = isString;
function isObject(target) {
    return getType(target) === 'object';
}
exports.isObject = isObject;
function isArray(target) {
    return getType(target) === 'array';
}
exports.isArray = isArray;
function analysisQueryString(url = window.location.href, isToLowerString = false) {
    let search = url.substring(url.lastIndexOf('?') + 1);
    const searchString = isToLowerString ? search.toLowerCase() : search;
    const obj = {};
    const reg = /([^?&=]+)=([^?&=]*)/g;
    searchString.replace(reg, (str, $1, $2) => {
        const key = decodeURIComponent($1);
        obj[key] = decodeURIComponent($2) + '';
        return str;
    });
    return obj;
}
exports.analysisQueryString = analysisQueryString;
function isHTMLElement(element) {
    try {
        return !!element.cloneNode(true) && element instanceof HTMLElement;
    }
    catch (e) {
        return false;
    }
}
exports.isHTMLElement = isHTMLElement;
function setStyle(element, cssObj) {
    if (isHTMLElement(element)) {
        Object.keys(cssObj || {}).forEach(function (key) {
            element.style[key] = cssObj[key];
        });
    }
    else {
        console.error(`${element} is not HTMLElement!`);
    }
}
exports.setStyle = setStyle;
function createElement(element, attrs, children) {
    const elementNode = document.createElement(element);
    if (attrs && typeof attrs === 'object') {
        for (let key in attrs) {
            if (key === 'style' && isType(attrs[key], 'object')) {
                setStyle(elementNode, attrs['style']);
            }
            else {
                elementNode.setAttribute(key, attrs[key]);
            }
        }
    }
    if (Array.isArray(children)) {
        children.forEach(function (child) {
            if (isHTMLElement(child)) {
                elementNode.appendChild(child);
            }
        });
    }
    return elementNode;
}
exports.createElement = createElement;
function createMediaElement(type, attrs = {}) {
    return createElement(type, {
        autoplay: true,
        controls: true,
        muted: false,
        ...attrs
    });
}
exports.createMediaElement = createMediaElement;
function hasChild(parentNode, childNode) {
    return (isHTMLElement(parentNode) &&
        isHTMLElement(childNode) &&
        parentNode.contains(childNode) &&
        childNode.parentNode === parentNode);
}
exports.hasChild = hasChild;
